package com.chandra.pos.config.security;

/**
 * Created by Admin on 6/25/14.
 * http://xebee.xebia.in/index.php/2014/02/25/spring-security-with-zero-xml/
 */

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


@Configuration
@EnableWebMvcSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {


//    @Autowired
//    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
//        auth
//                .inMemoryAuthentication()
//                .withUser("user").password("password").roles("USER");
//    }


    @Override
    protected void configure(AuthenticationManagerBuilder auth)
            throws Exception {
        auth.userDetailsService(userDetailService)
                .passwordEncoder(getPasswordEncoder()).and()
                .inMemoryAuthentication().withUser("admin").password("admin")
                .roles("ADMIN", "USER");
    }
//
//    @Override
//    protected void configure(AuthenticationManagerBuilder auth)
//            throws Exception {
//        auth
//                .inMemoryAuthentication().withUser("admin").password("admin")
//                .roles("ADMIN", "USER");
//    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable().authorizeRequests().antMatchers("/js/**", "/css/**", "/images/**", "/fonts/**","/resources/**", "/signup", "/logout", "/login")
                .permitAll().anyRequest()
                .authenticated().and().formLogin()
                .loginPage("/login").successHandler(successHandler()).permitAll().and().logout()
                .logoutSuccessUrl("/login?logout=success").logoutUrl("/logout");

        loadSystemRoles(http);


    }
    @Bean
    protected AuthenticationSuccessHandler successHandler(){
        SimpleUrlAuthenticationSuccessHandler simpleUrlAuthenticationSuccessHandler=new SimpleUrlAuthenticationSuccessHandler();
        simpleUrlAuthenticationSuccessHandler.setDefaultTargetUrl("/admin/home");
        return simpleUrlAuthenticationSuccessHandler;
    }

    public HttpSecurity loadSystemRoles(HttpSecurity http) throws Exception {


        http.authorizeRequests()
                .antMatchers("/js/**", "/css/**", "/images/**,/resources/**", "/signup", "/logout", "/login")
                .permitAll();


        Map<String, String> roles = new HashMap<>();
        roles.put("/admin/**", "ADMIN");
        roles.put("/customer/**", "CUSTOMER");
        roles.put("/products/**", "PRODUCT");
        roles.put("/employee/**", "EMPLOYEE");
        roles.put("/order/**", "ORDER");


        Iterator it = roles.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pair = (Map.Entry) it.next();

            http.authorizeRequests().antMatchers(pair.getKey().toString()).hasRole(pair.getValue().toString());

        }
        return http;
    }


    @Autowired
    private LocalUserDetailService userDetailService;


    @Bean
    public BCryptPasswordEncoder getPasswordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    public AuthenticationManager getAuthenticationManager() throws Exception {
        return super.authenticationManager();
    }
}
